package com.mce.nebula.controller;

import com.mce.nebula.dto.ApiResponse;
import com.mce.nebula.dto.UserDetailsResponse;
import com.mce.nebula.entity.User;
import com.mce.nebula.security.UserDetailsImpl;
import com.mce.nebula.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Slf4j
@RestController
@RequestMapping("/users")
@Tag(name = "用户详情", description = "用户详情相关接口")
@RequiredArgsConstructor
public class UserDetailsController {
    
    private final UserService userService;
    
    @GetMapping("/details")
    @Operation(summary = "查询我的详情", description = "查询当前登录用户的详细信息")
    public ResponseEntity<ApiResponse<UserDetailsResponse>> getUserDetails(
            Authentication authentication,
            @RequestParam(required = false, defaultValue = "") String teamName) {
        try {
            UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal();
            Optional<User> userOptional = userService.findByUsername(userDetails.getUsername());
            User user = userOptional.orElseThrow(() -> new RuntimeException("用户不存在"));
            
            UserDetailsResponse userDetail = new UserDetailsResponse();
            userDetail.setUserId(user.getId());
            userDetail.setUserName(user.getNickName());
            userDetail.setRealName(user.getRealName());
            userDetail.setLogo(user.getLogo());
            userDetail.setEmail(user.getEmail());
            userDetail.setEnterpriseId(user.getEnterpriseId());
            userDetail.setPhone(user.getPhone());
            userDetail.setIsSysAdmin(user.getSysAdmin());
            
            // TODO: 实现企业激活状态和管理员状态的检查
            userDetail.setIsEnterpriseActive(true);
            userDetail.setIsEnterpriseAdmin(user.getEnterpriseAdmin());
            
            // TODO: 实现角色和权限列表
            userDetail.setRoles(new ArrayList<>());
            userDetail.setPermissions(new ArrayList<>());
            
            // TODO: 实现团队信息
            userDetail.setTeams(new ArrayList<>());
            
            // TODO: 实现OAuth服务信息
            userDetail.setOauthServices(new ArrayList<>());
            
            return ResponseEntity.ok(ApiResponse.success(userDetail, "获取我的详情成功"));
        } catch (Exception e) {
            log.error("Failed to get user details", e);
            return ResponseEntity.badRequest()
                    .body(ApiResponse.error(400, "获取用户详情失败: " + e.getMessage()));
        }
    }
    
    @PostMapping("/details")
    @Operation(summary = "更新用户信息", description = "更新当前登录用户的信息")
    public ResponseEntity<ApiResponse<Void>> updateUserDetails(
            Authentication authentication,
            @RequestBody Map<String, String> requestData) {
        try {
            UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal();
            Long userId = userDetails.getId();
            
            String realName = requestData.get("real_name");
            String email = requestData.get("email");
            String logo = requestData.get("logo");
            
            userService.updateUser(userId, realName, null, logo);
            
            return ResponseEntity.ok(ApiResponse.success(null, "用户信息更新成功"));
        } catch (Exception e) {
            log.error("Failed to update user details", e);
            return ResponseEntity.badRequest()
                    .body(ApiResponse.error(400, "用户信息更新失败: " + e.getMessage()));
        }
    }
}